package com.ktgj.common.core.utils.helper;

import java.util.stream.Stream;

public class StringHelper
{
	public static boolean isNullOrWhiteSpace(String s)
	{
		return s == null || s.isEmpty() || s.trim().length() == 0;
	}

	public static boolean isNullOrEmpty(String s)
	{
		return s == null || s.isEmpty();
	}

	public static String trim(String str, String placeHoder)
	{
		return trimEnd(trimStart(str, placeHoder), placeHoder);
	}

	public static String trimStart(String str, String placeHoder)
	{
		if (isNullOrEmpty(str) || isNullOrEmpty(placeHoder)) return str;

		int to = 0;
		for (int i = 0; i < str.length(); i++)
		{
			String c = str.charAt(i) + "";
			if (!placeHoder.contains(c))
			{
				to = i;
				break;
			}
		}

		if (to == 0) return str;

		return str.substring(to);
	}

	public static String trimEnd(String str, String placeHoder)
	{
		if (isNullOrEmpty(str) || isNullOrEmpty(placeHoder)) return str;

		int to = 0;
		for (int i = str.length() - 1; i >= 0; i--)
		{
			String c = str.charAt(i) + "";
			if (!placeHoder.contains(c))
			{
				to = i;
				break;
			}
		}

		if (to == str.length() - 1) return str;

		return str.substring(0, to + 1);
	}

	public static String[] split(String str)
	{
		return split(str, ",|;");
	}

	public static String[] split(String str, String regex)
	{
		if (isNullOrEmpty(str)) return new String[0];

		return str.split(regex);
	}

	public static boolean contains(String ids, String id)
	{
		if (StringHelper.isNullOrEmpty(ids) || StringHelper.isNullOrEmpty(id)) return false;

		return Stream.of(ids.split(",")).anyMatch(x -> x.equals(id));
	}

	public static String id(String name)
	{
		if (StringHelper.isNullOrEmpty(name)) return name;

		return String.format("VAR_", name.replace("-", "_"));
	}
}
